#-------------------------------------------------------------------------------
# Name:        storeserver
# Purpose:     pyrex模块，服务器端扩展
#
# Author:      seewind
#
# Created:     28-02-2008
# Copyright:   (c) seewind 2008
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import WebStore.store as store

cdef extern from "python.h":
    int PyString_AsStringAndSize(object obj, char **s, Py_ssize_t *size)
    object PyString_FromStringAndSize(char *s, Py_ssize_t size)
    
cdef extern from "p2pserverexif.h":
    struct p2pserver_exinterface:
        void (*peer_state_changefunc)(char *peerid, int peer_state)
        void (*receive_datafunc) (char *peerid, char *data, int size)

cdef extern from "p2pserverif.h":
    struct p2pserver_interface:
        void (*senddatafunc) (char *peerid, char *data, int size)

cdef public p2pserver_interface global_p2pserver_interface

def senddata(peerid, data):
    cdef char *cdata
    cdef Py_ssize_t csize
    
    if global_p2pserver_interface.senddatafunc:
        if PyString_AsStringAndSize(data, &cdata, &csize) == 0:
            global_p2pserver_interface.senddatafunc(peerid, cdata, csize)

setattr(store, 'p2psenddata', senddata)

cdef public void peer_state_change(char *peerid, int peer_state):
    store.golbal_store.peerStateChange(peerid, peer_state) 

cdef public void receive_data(char *peerid, char *data, int size):
    store.golbal_store.receivedata(peerid, PyString_FromStringAndSize(data, size))
    
    
